import * as commonEnums from '../enums'
import { isObject,isArray } from './dataType'

/**表格合并自定义三层级子项
 * @param {Number, String, Array} item  需匹配的值
 * @param {string} type 要匹配的枚举对象名
 * @param {Props} selectOptions 自定义匹配的枚举对象
 * @return {Number, String} 匹配结果
 */
function getFormatValue(selectOptions: Array<Props> | any[] = [], item: any, type: string): string {
  const selectOptions_ = selectOptions.length
    ? selectOptions
    // @ts-ignore
    : commonEnums[type] || []
  if(selectOptions_.length) {
    if(isObject(selectOptions_[0])) {
      const target = selectOptions_.find((it: any) => {
        return it[item]
      })
      return target ? target[item] : item
    }
    return selectOptions_[item] || item
  }
  return item
}

export function mapFilter (item: any,type: string, selectOptions: Array<Props> = []): string {
  if (['',null,undefined].includes(item) || !type) {
    return ''
  }
  if (isArray(item)) {
    const filterStr: string = item.map((i: any) => {
      return getFormatValue(selectOptions, i, type)
    }).join('，')
    return filterStr
  }
  return getFormatValue(selectOptions, item, type)
}

// 金额过滤
export function formatMoney (origin: number| string, fractionDigits = 2) {
  if(origin === '' || origin === null || isNaN(Number(origin))) {
    return '-'
  }
  origin = typeof origin === 'number' ? origin : Number(origin)
  return origin.toLocaleString('en-Us',{
    minimumFractionDigits : fractionDigits,
    maximumFractionDigits : fractionDigits
  })
}

type Props = Record<string,any>

interface TransFormMapConfig {
  label: string;
  value: string;
  integer?: boolean;
}
// 枚举对象转数组
export function transformMap (map: Props,config : Props = {} as TransFormMapConfig) {
  const {
    label = 'label',
    value = 'value',
    integer = true
  } = config
  if(map instanceof Map) {
    const result = []
    for(const [key,val] of map) {
      result.push({
        [label]: val,
        [value]: integer ? parseInt(key) : key + ''
      })
    }
    return result
  } else {
    return Object.keys(map).reduce((result,key)=>{
      result.push({
        [label]: map[key],
        [value]: integer ? parseInt(key) : key + ''
      })
      return result
    }, [] as Props[])
  }
}

function padLeftZero (str: string) {
  return ('00'+str).substring(str.length)
}

// 日期过滤
export const formatDate = (date: number| Date,fmt = 'yyyy-MM-dd hh:mm') => {
  if(date === 0 || !date) {
    return ''
  }
  if(!(date instanceof Date)) {
    date = new Date(date)
    if(date.toString()=== 'Invalid Date') {
      console.warn('params date can not be transform into Date')
    }
  }

  if(/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substring(4 - RegExp.$1.length))
  }
  interface Io {
    [x: string]: number
  }
  const o: Io = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate() + 1,
    'h+': date.getHours() + 1,
    'm+': date.getMinutes() + 1,
    's+': date.getSeconds() + 1,
  }
  for(const k in o) {
    if(new RegExp(`(${k})`).test(fmt)) {
      const str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
    }
  }
  return fmt
}
